package me.gavincook.moon.plugin.action;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;
import com.intellij.psi.impl.source.PsiClassReferenceType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;

import me.gavincook.moon.plugin.util.MoonConstants;

/**
 * Java Bean转化为json字符串
 *
 * @author gavincook
 * @version $ID: JavaBean2JsonAction.java, v0.1 2018-05-03 15:02 gavincook Exp $$
 */
public class JavaBean2JsonAction extends AnAction {

    private static final String DOUBLE_QUOTE = "\"";

    private static final String COLON        = ":";

    private static final String LINE_FEED    = "\n";

    private static final String COMMA        = ",";

    private static final String TAB_IN_SPACE = "    ";

    @Override
    public void actionPerformed(AnActionEvent e) {
        PsiFile psiFile = e.getData(PlatformDataKeys.PSI_FILE);
        PsiElement psiElement = psiFile.getOriginalElement();
        PsiClass psiClass = PsiTreeUtil.getChildOfType(psiElement, PsiClass.class);

        PsiField[] fields = psiClass.getAllFields();
        StringBuilder json = new StringBuilder();
        json.append("{");
        //是否需要追加逗号
        boolean needComma = false;
        for (PsiField field : fields) {
            //处理逗号
            if (needComma) {
                json.append(COMMA);
            } else {
                needComma = true;
            }
            json.append(LINE_FEED).append(TAB_IN_SPACE).append(DOUBLE_QUOTE).append(field.getName())
                .append(DOUBLE_QUOTE).append(COLON);
            PsiType type = field.getType();
            if (type instanceof PsiPrimitiveType) {//原始类型
                json.append(PsiTypesUtil.getDefaultValueOfType(type));
            } else if (type instanceof PsiClassReferenceType) {//引用类型
                //todo 兼容其他引用类型
                //处理字符串
                if (String.class.getName().equals(type.getCanonicalText())) {
                    json.append(DOUBLE_QUOTE).append(DOUBLE_QUOTE);
                }
            }
        }
        json.append(LINE_FEED).append("}");

        //复制到粘贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection(json.toString()), null);

        //显示成功提示
        Notification notification = new Notification(MoonConstants.NOTIFICATION_GROUP_ID, "提示",
            "Java Bean已经成功生成JSON结构，并复制到粘贴板.", NotificationType.INFORMATION);
        Notifications.Bus.notify(notification);
    }

}
